using System;
using System.Data;
using System.Web;
using System.Web.UI.WebControls;
using FormsGenerator.DataAccess;

namespace FormsGenerator
{
	/// <summary>
	/// Summary description for FormAccess.
	/// </summary>
	public partial class FormAccess : System.Web.UI.Page
	{
		protected System.Data.DataView dataViewShowAccess;

		protected void Page_Load(object sender, System.EventArgs e)
		{
			// Put user code to initialize the page here
			if (ViewState["GUID"] == null)
			{
				if (Request["id"] == null || Request["id"].Equals(""))
				{
					LabelErrorMessage.Visible = true;
					LabelErrorMessage.Text += "No valid form id!";
				}
				else
				{
					ViewState["GUID"] = Request["id"].Trim();
					if (!IsPostBack)
					{
						BindGrid();
						PanelFormAccess.Visible = true;
					}
				}
			}

		}

		#region Web Form Designer generated code

		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}

		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.dataViewShowAccess = new System.Data.DataView();
			((System.ComponentModel.ISupportInitialize) (this.dataViewShowAccess)).BeginInit();
			this.DataGridFormAccess.CancelCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGridFormAccess_CancelCommand);
			this.DataGridFormAccess.EditCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGridFormAccess_EditCommand);
			this.DataGridFormAccess.UpdateCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGridFormAccess_UpdateCommand);
			this.DataGridFormAccess.DeleteCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGridFormAccess_DeleteCommand);
			this.DataGridFormAccess.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.DataGridFormAccess_ItemDataBound);
			((System.ComponentModel.ISupportInitialize) (this.dataViewShowAccess)).EndInit();

		}

		#endregion

		private void DataGridFormAccess_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
		{
			DataGridFormAccess.EditItemIndex = e.Item.ItemIndex;
			BindGrid();
		}

		private void BindGrid()
		{
			DataTable dtFormsAccess = DBFactory.Instance.DBHandlerInstance().GetFormAccess((string) ViewState["GUID"], HttpContext.Current.User.Identity.Name);
			dataViewShowAccess = new DataView(dtFormsAccess, "", "UserId", DataViewRowState.CurrentRows);

			//Check if the Form is shared to all user. If so, then do not display shared users, 
			//only display a label saying that the form is shared to all users
			DataRowView[] foundSharedToAll = dataViewShowAccess.FindRows(new object[] {"all"});
			if (foundSharedToAll.Length > 0)
			{
				DataGridFormAccess.Visible = false;
				LabelSharedToAll.Visible = true;
				LabelSharedToNone.Visible = false;
				CheckBoxAll.Checked = true;
				TextBoxUsername.Enabled = false;
			}
			else
			{
				//Show Datagrid if it has values. 
				if (dataViewShowAccess.Count > 0)
				{
					DataGridFormAccess.DataSource = dataViewShowAccess;
					DataGridFormAccess.DataBind();
					LabelSharedToAll.Visible = false;
					LabelSharedToNone.Visible = false;
					DataGridFormAccess.Visible = true;
					TextBoxUsername.Enabled = true;
				}
					//Show label saying ut's not shared to anyone
				else
				{
					LabelSharedToNone.Visible = true;
					LabelSharedToAll.Visible = false;
					DataGridFormAccess.Visible = false;
					TextBoxUsername.Enabled = true;
				}

			}
		}

		private void DataGridFormAccess_CancelCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
		{
			DataGridFormAccess.EditItemIndex = -1;
			BindGrid();
		}

		private void DataGridFormAccess_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
		{
			TableCell itemCell = e.Item.Cells[1];
			string strUserToBeRemovedId = itemCell.Text;
			try
			{
				DBFactory.Instance.DBHandlerInstance().RemoveFormAccess((string) ViewState["GUID"], HttpContext.Current.User.Identity.Name, strUserToBeRemovedId);
			}
			catch (Exception ex)
			{
				LabelErrorMessage.Text = ex.Message;
				LabelErrorMessage.Visible = true;
			}
			BindGrid();
			LabelConfirmMessage.Text = "Removed user <b>" + itemCell.Text + "</b>!<br />";
			LabelConfirmMessage.Visible = true;
		}

		private void DataGridFormAccess_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
		{
			String strUserToUpdateId = ((TextBox) e.Item.Cells[1].Controls[0]).Text;
			String strFormsAccessId = e.Item.Cells[0].Text;
			try
			{
				DBFactory.Instance.DBHandlerInstance().UpdateFormAccess((string) ViewState["GUID"], HttpContext.Current.User.Identity.Name, strUserToUpdateId, strFormsAccessId);
				// Set the EditItemIndex property to -1 to exit editing mode. 
				// Be sure to rebind the DateGrid to the data source to refresh
				// the control.
			}
			catch (Exception ex)
			{
				LabelErrorMessage.Text = ex.Message;
				LabelErrorMessage.Visible = true;
			}
			DataGridFormAccess.EditItemIndex = -1;
			BindGrid();
		}

		protected void ButtonUpdate_Click(object sender, System.EventArgs e)
		{
			BindGrid();
		}

		protected void CheckBoxAll_CheckedChanged(object sender, System.EventArgs e)
		{
			try
			{
				TextBoxUsername.Text = "";
				if (CheckBoxAll.Checked)
				{
					string strUserToAddId = "all";
					DBFactory.Instance.DBHandlerInstance().InsertFormAccess((string) ViewState["GUID"], HttpContext.Current.User.Identity.Name, strUserToAddId);
				}
				else
				{
					string strUserToBeRemovedId = "all";
					DBFactory.Instance.DBHandlerInstance().RemoveFormAccess((string) ViewState["GUID"], HttpContext.Current.User.Identity.Name, strUserToBeRemovedId);
				}
			}
			catch (Exception ex)
			{
				LabelErrorMessage.Text = ex.Message;
				LabelErrorMessage.Visible = true;
			}
		}

		protected void TextBoxUsername_TextChanged(object sender, System.EventArgs e)
		{
			string strUserToAddId = TextBoxUsername.Text.Trim();
			if (!strUserToAddId.Equals(""))
			{
				try
				{
					DBFactory.Instance.DBHandlerInstance().InsertFormAccess((string) ViewState["GUID"], HttpContext.Current.User.Identity.Name, strUserToAddId);
				}
				catch (Exception ex)
				{
					LabelErrorMessage.Text = ex.Message;
					LabelErrorMessage.Visible = true;
				}
				//BindGrid();	
			}
		}

		private void DataGridFormAccess_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
		{
			/*
			DataGridFormAccess.Columns[1].
			((LinkButton)e.Item.Cells[1].Controls[1]).Attributes["onClick"]="return confirm(\"Are you sure you want to delete the user?\")";
			*/
		}
	}
}